package com.artron.mediaartron.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.artron.mediaartron.base.BaseFragmentPagerAdapter;
import com.artron.mediaartron.ui.fragment.made.picture.PictureNetFragment;
import com.artron.mediaartron.ui.fragment.made.picture.PictureSelectionFragment;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-27.
 */

public class PictureTabAdapter extends BaseFragmentPagerAdapter {

    private final FragmentManager mFragmentManager;
    private ViewGroup mContainer;
    private PictureSelectionFragment mBoxingFragment;

    public PictureTabAdapter(FragmentManager fm, PictureSelectionFragment fragment) {
        super(fm);
        this.mFragmentManager = fm;
        this.mBoxingFragment = fragment;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mContainer == null) {
            mContainer = container;
        }
        return super.instantiateItem(container, position);
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return mBoxingFragment;
        } else {
            return PictureNetFragment.newInstance();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    public Fragment getFragment(int position) {
        String tag = makeFragmentName(mContainer.getId(), getItemId(position));
        return mFragmentManager.findFragmentByTag(tag);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return "本地";
        } else {
            return "素材";
        }
    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }
}
